package cn.doctor.assistant.pad.api.bean;

import com.alibaba.fastjson.JSON;

import org.json.JSONException;
import org.json.JSONObject;

import cn.doctor.assistant.pad.api.AssistAPI;

/**
 * Created by jiaolf on 2016/3/23.
 */
public class AssResponse {
    private String jsonrpc;
    private String method;
    private String clientInfo;
    private Result result;

    public boolean isSuccess() {
        return result != null && result.getResultStatus().equals(AssistAPI.STATUS_CODE_SUCCESS);
    }

    /**
     * 获取result中的String Value
     */
    public String getJsonValue(String key){
        try {
            JSONObject jsonObject = new JSONObject(result.getData());
            return  jsonObject.getString(key);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取result中的value
     * @param key
     * @param clazz 获取的value类型
     * @return
     */
    public Object getJsonValue(String key, Class<?> clazz) {
        try {
            JSONObject jsonObject = new JSONObject(result.getData());

            if (clazz == Integer.class) {
                return jsonObject.getInt(key);
            } else if (clazz == Boolean.class) {
                return jsonObject.getBoolean(key);
            } else if (clazz == String.class) {
                return jsonObject.getString(key);
            } else if (clazz == Long.class) {
                return jsonObject.getLong(key);
            } else if (clazz == Float.class || clazz == Double.class) {
                return jsonObject.getDouble(key);
            } else {
                return jsonObject.get(key);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return "";


    }

    public class Result {
        private String resultStatus;
        private String info;
        private String data;

        public String getResultStatus() {
            return resultStatus;
        }

        public void setResultStatus(String resultStatus) {
            this.resultStatus = resultStatus;
        }

        public String getInfo() {
            return info;
        }

        public void setInfo(String info) {
            this.info = info;
        }

        public String getData() {
            return data;
        }

        public void setData(String data) {
            this.data = data;
        }
    }

    public String getJsonrpc() {
        return jsonrpc;
    }

    public void setJsonrpc(String jsonrpc) {
        this.jsonrpc = jsonrpc;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getClientInfo() {
        return clientInfo;
    }

    public void setClientInfo(String clientInfo) {
        this.clientInfo = clientInfo;
    }

    public Result getResult() {
        return result;
    }

    public void setResult(Result result) {
        this.result = result;
    }


    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
